package health.lm.com.component_base.popup.sign;

import android.content.Context;
import android.os.Build;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.daimajia.androidanimations.library.Techniques;
import com.daimajia.androidanimations.library.YoYo;

import java.util.ArrayList;
import java.util.List;

import health.lm.com.component_base.R;
import health.lm.com.component_base.popup.model.PopupModel;
import health.lm.com.component_base.util.PlayAudioHelper;
import health.lm.com.component_base.widget.CustomPopWindow;
import health.lm.com.network.callback.SimpleCallBack;

/**
 * 签到弹窗
 */

public class SignPopUtil {
    private static SignPopUtil sharePopUtil;
    private static Context ctx;
    private CustomPopWindow popWindow;
    private LinearLayout rl_sign;
    private TextView tv_sign_date;
    private TextView tv_sign_num;
    private TextView tv_energy_chinese;
    private TextView sign;
    private LinearLayout rl_signed;
    private LinearLayout ll_content;
    private RelativeLayout rl_main;
    private SignPopAdapter adapter;

    private boolean homeEntrance = false;

    public static SignPopUtil getInstance() {
        if (sharePopUtil == null)
            sharePopUtil = new SignPopUtil();
        return sharePopUtil;
    }

    /*home入口*/
    public void initHome(Context context, View mLayoutPopup) {
        homeEntrance = true;
        init(context, mLayoutPopup, "", new ArrayList<>());
    }

    public void init(Context context, View mLayoutPopup) {
        homeEntrance = false;
        init(context, mLayoutPopup, "", new ArrayList<>());
    }

    /**
     * 显示popwindow
     */
    public void init(Context context, View mLayoutPopup, String id, List<SignPopBean> beanList) {
        ctx = context;
        View contentView = LayoutInflater.from(context).inflate(R.layout.dialog_sign, null);
        RecyclerView rlv_sign = contentView.findViewById(R.id.rlv_sign);
        adapter = new SignPopAdapter(beanList);
        rlv_sign.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));
        rlv_sign.setAdapter(adapter);
        //签到按钮
        sign = contentView.findViewById(R.id.btn_sign);
        sign.setOnClickListener(v -> signed());
        //主布局
        rl_main = contentView.findViewById(R.id.rl_main);
        rl_main.setOnClickListener(v -> popWindow.dissmiss());
        //签到布局
        rl_sign = contentView.findViewById(R.id.rl_sign);
        //显示签到内容
        rl_signed = contentView.findViewById(R.id.rl_signed);
        //显示签到内容
        ll_content = contentView.findViewById(R.id.ll_content);
        //币名称
        tv_sign_date = contentView.findViewById(R.id.tv_sign_date);
        //币数量
        tv_sign_num = contentView.findViewById(R.id.tv_sign_num);
        //说明
        tv_energy_chinese = contentView.findViewById(R.id.tv_energy_chinese);

        ImageView imageView = contentView.findViewById(R.id.imageView);
        YoYo.with(Techniques.FadeIn)
                .duration(700)
                .repeat(YoYo.INFINITE)
                .pivot(YoYo.CENTER_PIVOT, YoYo.CENTER_PIVOT)
                .interpolate(new AccelerateDecelerateInterpolator())
                .playOn(imageView);

//        RequestOptions options = new RequestOptions()
//                .diskCacheStrategy(DiskCacheStrategy.RESOURCE);
//        Glide.with(ctx).load(R.mipmap.light).apply(options).into(imageView);
//        Animation animation = AnimationUtils.loadAnimation(ctx, R.anim.rotate);
//        animation.setInterpolator(new LinearInterpolator());
//        animation.setDetachWallpaper(false);
//        imageView.startAnimation(animation);
        PopupModel.getInstance().getSignInfo(new SimpleCallBack<SignPopResultBean>() {
            @Override
            public void onSuccess(SignPopResultBean pSignPopResultBean) {
                adapter.setNewData(pSignPopResultBean.getData());
                if (pSignPopResultBean.getStatus().equals("0")) {
                    sign.setText("立即签到");
                    //sign.setOnClickListener(v-> signed());
                } else {
                    if (homeEntrance) return;
                    setAnimation();
                    isSigned(pSignPopResultBean);
                }
                //创建并显示popWindow显示大小
                popWindow = new CustomPopWindow.PopupWindowBuilder(context)
                        .setView(contentView)
                        .enableBackgroundDark(true)
                        .setBgDarkAlpha(0.3f)
                        .setAnimationStyle(R.style.pop_bottom_anim)
                        .size(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)//显示大小
                        .create()
                        .showAtLocation(mLayoutPopup, Gravity.CENTER, 0, 0);
                //判断是否升级
                //UpLevelPopUtil.getInstance().init(context, mLayoutPopup, baseView);
            }
        });
    }

    private void signed() {
        setAnimation();
        PopupModel.getInstance().getSigned(new SimpleCallBack<SignPopResultBean>() {
            @Override
            public void onSuccess(SignPopResultBean pSignPopResultBean) {
                PlayAudioHelper.getInstance().play(ctx, PlayAudioHelper.RING);
                isSigned(pSignPopResultBean);
            }
        });
    }

    /**
     * 签到后数据填充
     *
     * @param entity 数据源
     */
    private void isSigned(SignPopResultBean entity) {
        adapter.setNewData(entity.getData());
        tv_sign_date.setText(entity.getTip());
        tv_sign_num.setText(entity.getSugar());
        StringBuilder builder = new StringBuilder();
        builder.append(entity.getTime()).append("\n").append(entity.getStr());
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            tv_energy_chinese.setText(Html.fromHtml(builder.toString(), Html.FROM_HTML_MODE_COMPACT));
        } else {
            tv_energy_chinese.setText(Html.fromHtml(builder.toString()));
        }
    }

    private void setAnimation() {
        rl_signed.setVisibility(View.VISIBLE);
        rl_sign.setVisibility(View.GONE);

        Animation alpha = new AlphaAnimation(0, 1);
        alpha.setDuration(600);
        ll_content.startAnimation(alpha);
        Animation alpha2 = new AlphaAnimation(0, 1);
        alpha2.setStartOffset(200);
        alpha2.setDuration(600);
        tv_energy_chinese.startAnimation(alpha2);
    }
}
